package com.app.service.impl;

import com.app.mapper.ScoresMapper;
import com.app.model.ScoresModel;
import com.app.service.ScoresService;
import com.app.vo.ScoresVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class ScoresServiceImpl implements ScoresService {
   @Autowired
    ScoresMapper scoresMapper;

    @Override
    public ScoresModel getScoreById(Integer id) {
        return scoresMapper.selectById(id);
    }

    @Override
    public List<ScoresModel> getScoresListByUserId(Integer userId) {
        QueryWrapper<ScoresModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("user_id", userId);
        queryWrapper.orderByDesc("after_score");
        List<ScoresModel> scoresModelList = scoresMapper.selectList(queryWrapper);
        return scoresModelList;
    }

    @Override
    public List<ScoresModel> getScoresList(Integer userId, Integer operateType) {
        QueryWrapper<ScoresModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("user_id", userId).eq("operate_type",operateType);
        queryWrapper.orderByDesc("crt_time");
        List<ScoresModel> scoresModelList = scoresMapper.selectList(queryWrapper);
        return scoresModelList;
    }

    @Override
    public Page<ScoresModel> getPageScoresById(Integer page, Integer limit, Integer userId) {
        QueryWrapper<ScoresModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("user_id", userId);
        queryWrapper.orderByDesc("crt_time");
        Page<ScoresModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<ScoresModel> scoreList = scoresMapper.selectPage(pages,queryWrapper);
        return scoreList;
    }

    @Override
    public void update(ScoresModel model) {scoresMapper.updateById(model);
    }

    @Override
    public void insert(ScoresModel model) {
        scoresMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        scoresMapper.deleteById(id);
    }

    @Override
    public List<ScoresVo> selectScoresSort() {
        return scoresMapper.selectScoresSort();
    }

    @Override
    public ScoresModel getScoreByUserId(Integer userId) {
        QueryWrapper<ScoresModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("user_id", userId);
        queryWrapper.orderByDesc("crt_time");
        List<ScoresModel> list = scoresMapper.selectList(queryWrapper);
        if(list!=null&&list.size()>0)
        {
            return list.get(0);
        }else{
            return null;
        }

    }

}
